package cn.cnlee.app;

import android.os.Bundle;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import cn.cnlee.app.config.Constants;
import cn.cnlee.app.ui.auction.AuctionFragment;
import cn.cnlee.app.ui.future.FutureFragment;
import cn.cnlee.app.ui.home.HomeFragment;
import cn.cnlee.app.ui.mine.MineFragment;
import cn.cnlee.app.ui.spot.SpotFragment;
import cn.cnlee.arch.base.BaseActivity;
import cn.cnlee.arch.base.BaseFragment;
import cn.cnlee.arch.util.ClassUtil;

public class MainActivity extends BaseActivity {

    private BaseFragment[] cacheFragmentArr = new BaseFragment[Tabs.values().length];

    @Override
    public int getLayoutId() {
        return R.layout.main_activity_layout;
    }

    @Override
    public void initViews(Bundle savedInstanceState) {
        loadManager.showSuccess();
        //init Bottom tabBar
        initNavBar();
        //init fragment
        initFragment(0);
    }

    private void initNavBar() {
        BottomNavigationBar mBottomNavigationBar = findViewById(R.id.bottom_navigation_bar);
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        for (Tabs value : Tabs.values()) {
            mBottomNavigationBar.addItem(makeBottomNavigationItem(value));
        }
        mBottomNavigationBar.setFirstSelectedPosition(0).initialise();
        mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                initFragment(position);
            }

            @Override
            public void onTabUnselected(int position) {
            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }

    private void initFragment(int i) {
        FragmentManager mFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        showFragment(fragmentTransaction, i);
        fragmentTransaction.commit();
    }

    private void showFragment(FragmentTransaction fragmentTransaction, int index) {
        BaseFragment fragment = cacheFragmentArr[index];
        if (fragment == null) {
            Tabs tab = Tabs.values()[index];
            fragment = ClassUtil.getNewInstance(tab.cls);
            cacheFragmentArr[index] = fragment;
            fragmentTransaction.add(R.id.home_content, fragment, tab.tag);
        } else {
            fragmentTransaction.show(fragment);
        }
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        for (BaseFragment fragment : cacheFragmentArr) {
            if (fragment != null) {
                fragmentTransaction.hide(fragment);
            }
        }
    }

    private BottomNavigationItem makeBottomNavigationItem(Tabs tabs) {
        return new BottomNavigationItem(tabs.iconResId, tabs.titleResId).setActiveColorResource(R.color.colorPrimary);
    }

    public enum Tabs {

        HOME(HomeFragment.class, R.string.home_title_name, R.mipmap.tab_home_icon, Constants.HOME_TAG),
        SPOT(SpotFragment.class, R.string.spot_title_name, R.mipmap.tab_spot_icon, Constants.SPOT_TAG),
        FUTURE(FutureFragment.class, R.string.future_title_name, R.mipmap.tab_future_icon, Constants.FUTURE_TAG),
        AUCTION(AuctionFragment.class, R.string.auction_title_name, R.mipmap.tab_auction_icon, Constants.AUCTION_TAG),
        MINE(MineFragment.class, R.string.mine_title_name, R.mipmap.tab_mine_icon, Constants.MINE_TAG);

        private int titleResId;
        private int iconResId;
        private String tag;
        private Class cls;

        Tabs(Class cls, int titleResId, int iconResId, String tag) {
            this.cls = cls;
            this.titleResId = titleResId;
            this.iconResId = iconResId;
            this.tag = tag;
        }
    }
}
